package com.softstao.guoyu.ui.activity.sale;

import android.content.Intent;
import android.support.v7.app.AlertDialog;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;

import com.softstao.guoyu.R;
import com.softstao.guoyu.global.SharePreferenceManager;
import com.softstao.guoyu.model.sale.OrderGoods;
import com.softstao.guoyu.model.sale.Retail;
import com.softstao.guoyu.model.sale.RetailList;
import com.softstao.guoyu.mvp.interactor.sale.RetailInteractor;
import com.softstao.guoyu.mvp.presenter.sale.RetailPresenter;
import com.softstao.guoyu.mvp.viewer.sale.DeleteRetailViewer;
import com.softstao.guoyu.mvp.viewer.sale.RetailListViewer;
import com.softstao.guoyu.ui.activity.BaseListActivity;
import com.softstao.guoyu.ui.adapter.OrderGoodsAdapter;
import com.softstao.guoyu.ui.baseAdapter.RecycleViewBaseAdapter;
import com.softstao.guoyu.ui.baseAdapter.RecycleViewHolder;
import com.softstao.softstaolibrary.library.utils.LZUtils;
import com.softstao.softstaolibrary.library.widget.FullyLinearLayoutManager;
import com.softstao.softstaolibrary.library.widget.LZToast;
import com.softstao.softstaolibrary.library.widget.MarginDecoration2;
import com.wangjie.androidinject.annotation.annotations.mvp.AIPresenter;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

/**
 * Created by vaikus on 2017/7/11.
 */

public class RetailListActivity extends BaseListActivity<Retail> implements RetailListViewer,DeleteRetailViewer{

    @AIPresenter(presenter = RetailPresenter.class,interactor = RetailInteractor.class)
    RetailPresenter presenter;

    private SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    private int position;

    @Override
    public void initView() {
        super.initView();
        initTitle("零售单");
        titleBar.setRightIcon(R.mipmap.add_btn);
        titleBar.setRightIconOnClick(v -> startActivity(new Intent(context,ChooseProductActivity.class)));

        adapter = new RecycleViewBaseAdapter<Retail>(datas,R.layout.layout_order_list_item) {
            @Override
            public void convert(RecycleViewHolder holder, Retail retail) {
                RecyclerView.LayoutParams layoutParams = new RecyclerView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
                holder.itemView.setLayoutParams(layoutParams);
                holder.getView(R.id.right).setVisibility(View.GONE);
                holder.setText(R.id.order_sn, "零售单编号：" + retail.getRetailOrderCode())
                        .setText(R.id.time,"时间：" + format.format(new Date(retail.getDate())))
                        .setText(R.id.from_agent, "买家：" + retail.getName())
                        .setText(R.id.total_price, mContext.getResources().getString(R.string.rmb) + LZUtils.priceFormat(retail.getMoney()));
                RecyclerView recyclerView = holder.getView(R.id.list_view);
                OrderGoodsAdapter adapter;
                if (retail.getProductList()==null){
                    adapter = new OrderGoodsAdapter(new ArrayList<OrderGoods>());
                }else {
                    adapter = new OrderGoodsAdapter(retail.getProductList());
                }
                recyclerView.setTag(adapter);
                recyclerView.setAdapter(adapter);
                recyclerView.setLayoutManager(new FullyLinearLayoutManager(holder.itemView.getContext()));
                adapter.setListener(position -> startActivity(new Intent(context,RetailDetailActivity.class).putExtra("code",retail.getRetailOrderCode())));
                holder.getView(R.id.btn1).setVisibility(View.VISIBLE);
                holder.setText(R.id.btn1,"删除");
                holder.getView(R.id.btn1).setOnClickListener(v ->
                    new AlertDialog.Builder(context)
                            .setMessage("确定删除此零售单？")
                            .setPositiveButton("确定",(dialog, which) -> {
                                dialog.dismiss();
                                position = holder.getAdapterPosition();
                                deleteRetail();
                            }).setNegativeButton("取消",(dialog, which) -> dialog.dismiss())
                            .create().show()
                );
            }
        };
        adapter.setListener(position -> startActivity(new Intent(context,RetailDetailActivity.class).putExtra("code",datas.get(position).getRetailOrderCode())));
        mRecycleView.setAdapter(adapter);
        mRecycleView.setLayoutManager(new FullyLinearLayoutManager(this));
        mRecycleView.addItemDecoration(new MarginDecoration2(this));
    }

    @Override
    protected void onResume() {
        super.onResume();
        findRetails();
    }

    @Override
    public void onRefresh() {
        super.onRefresh();
        findRetails();
    }

    @Override
    public void onLoad() {
        super.onLoad();
        findRetails();
    }

    @Override
    public void findRetails() {
        loading.setVisibility(View.VISIBLE);
        presenter.getRetailList(currentPage, SharePreferenceManager.getInstance().getAgentId());
    }

    @Override
    public void getRetailsList(RetailList retailList) {
        loading.setVisibility(View.GONE);
        if (retailList!=null){
            if (retailList.getRetailOrderList()!=null&&retailList.getRetailOrderList().size()!=0){
                emptyLayout.setVisibility(View.GONE);
                more.setVisibility(View.GONE);
                if (currentPage==1) {
                    datas.clear();
                }
                datas.addAll(retailList.getRetailOrderList());
                adapter.notifyDataSetChanged();
            }else {
                if (currentPage==1){
                    isEmpty();
                }else {
                    more.setVisibility(View.VISIBLE);
                }
            }
        }
    }

    @Override
    protected void error(String msg) {
        super.error(msg);
        loading.setVisibility(View.GONE);
    }

    @Override
    public void deleteRetail() {
        loading.setVisibility(View.VISIBLE);
        presenter.delete(SharePreferenceManager.getInstance().getAgentId(),datas.get(position).getRetailOrderCode());
    }

    @Override
    public void deleteResult(Object result) {
        datas.remove(position);
        adapter.notifyItemRemoved(position);
        LZToast.getInstance(context).showToast("删除成功");
    }
}
